package com.xxx.convert;

import com.alibaba.druid.util.StringUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/20 9:34
 * @Copyright 博为峰
 */
//类型转换器
public class StringToDate implements Converter<String, Date> {

    @Override
    public Date convert(String s) {
        SimpleDateFormat format = null;
        try {
            if (StringUtils.isEmpty(s)) {
                throw new NullPointerException("请输入正确的数据");
            }
            format = new SimpleDateFormat("yyyy-MM-dd");
            final Date date = format.parse(s);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException("请输入正确的日期格式(yyyy-MM-dd)");
        }
    }
}
